<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-16
 * Time: ����4:09
 */
include '/var/www/html/new/d/module/qkxproject/api/des/Curl.class.php';
class  ZiWoYou{
    function  __construct(){
          $this->url ='http://u.dgkangfei.com/api/order.jsp';
        $this->custId='291009';
        $this->apikey='E885A0E22E64EB8D38CC7338407E997B';
    }
    function order($request_arr){
        $request_arr1['custId']=$this->custId;
        $request_arr1['apikey']=$this->apikey;
        $xml="<order><travel_date>{$request_arr['travel_date']}</travel_date><info_id>{$request_arr['info_id']}</info_id><user_id></user_id><cust_id>{$this->custId}</cust_id><get_type>0</get_type><order_source_id>{$request_arr['order_source_id']}</order_source_id><order_memo></order_memo><num>{$request_arr['num']}</num><link_man>{$request_arr['link_man']}</link_man><link_phone>{$request_arr['link_phone']}</link_phone><link_address></link_address><link_code></link_code><link_email>33333333@qq.com</link_email><link_credit_type></link_credit_type><link_credit_no></link_credit_no><peoples></peoples><conds></conds></order>";
        $request_arr1['param']=$xml;

//        print_r($request_arr1);

        $result = Cu::curl($this->url,$request_arr1,$type = 'get');
//        $this->logfile('下单记录'.$result,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/ziwoyou_get_data.txt');
        $result = simplexml_load_string( $result, 'SimpleXMLElement', LIBXML_NOCDATA );
        $status = (string)$result->status;
        $order_id = (string)$result->order_id;
        $msg = (string)$result->msg;
        $error_state =  (string)$result->error_state;
        $error_msg =  (string)$result->error_msg;
        if($status==1){
            $success = $this->pay($order_id);
            if($success==100){
                $this->logfile(date('Y-m-d,H:i:s').'下单成功'.'|'.$order_id.'|'.$status.'|'.$msg,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/order_suc.txt');
                $arr['orderid'] = $order_id;
                $arr['ls_order'] = $request_arr['order_source_id'];
                return $arr;
            }else{
                $this->logfile(date('Y-m-d,H:i:s').'下单失败'.'|'.$order_id.'|'.$status.'|'.$error_state.'|'.$error_msg,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/order_fail.txt');
                return 101;
            }

        }
    }
        function pay($order_id){

            $arr['custId'] = $this->custId;
            $arr['apikey']=$this->apikey;
            $arr['orderId']=$order_id;
            $result = Cu::curl('http://u.dgkangfei.com/api/pay.jsp',$arr,$type = 'get');
//            print_r($result);
//            $this->logfile('支付记录'.$result,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/ziwoyou_get_data.txt');
            $result = simplexml_load_string( $result, 'SimpleXMLElement', LIBXML_NOCDATA );
            $order_id  = (string)$result->order_id;
            $status = (string)$result->status;
            $msg = (string)$result->msg;
            $error_state =  (string)$result->error_state;
            $error_msg =  (string)$result->error_msg;
            if($status==1){
                $this->logfile(date('Y-m-d,H:i:s').'支付成功'.'|'.$order_id.'|'.$status.'|'.$msg,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/order_suc.txt');
               return 100;
            }else{

                $this->logfile(date('Y-m-d,H:i:s').'支付失败'.'|'.$status.'|'.$error_state.'|'.$error_msg,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/order_fail.txt');
                return 101;
            }
        }
    function canceOrder($UUtordernum){
        $arr['custId'] = $this->custId;
        $arr['apikey']=$this->apikey;
        $arr['orderId']=$UUtordernum;
        $result = Cu::curl('http://u.dgkangfei.com/api/cancelOrder.jsp',$arr,$type = 'get');
//            print_r($result);
//        $this->logfile('支付记录'.$result,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/ziwoyou_get_data.txt');
        $result = simplexml_load_string( $result, 'SimpleXMLElement', LIBXML_NOCDATA );
        $order_id  = (string)$result->order_id;
        $status = (string)$result->status;
        $msg = (string)$result->msg;
        $error_state =  (string)$result->error_state;
        $error_msg =  (string)$result->error_msg;
        if($status==1){
            $this->logfile(date('Y-m-d,H:i:s').'退票成功'.'|'.$order_id.'|'.$status.'|'.$msg,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/order_suc.txt');
            return 100;
        }else{

            $this->logfile(date('Y-m-d,H:i:s').'退票失败'.'|'.$status.'|'.$error_state.'|'.$error_msg,'/var/www/html/new/d/module/qkxproject/api/ziwoyou/order_fail.txt');
            return 101;
        }
    }


    function logfile($txt, $path)
    {
        $fp = fopen($path, "a");
        flock($fp, LOCK_EX);
        fwrite($fp, $txt . "\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }


}
